<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // DateFormat 日期格式化  => 可以指定输出格式

    // 2022-07-23 17:00:00
    // 2022-07-24 05:59
    // 2022-07-28
    // 2022-07
    // 16:51:22

    // pattern  期望输出的日期格式 "YYYY-MM-DD hh:mm:ss"
    // time     自己指定时间 (日期对象/日期字符串/毫秒数)
    function DateFormat(pattern, time) {
        // console.log(pattern);  // "YYYY-MM-DD hh:mm:ss"

        if (time != undefined) { // 没有参数
            var date = new Date(time);
        } else {  //没有参数
            var date = new Date();
        }

        var year = date.getFullYear();
        
        var month = date.getMonth() + 1;
        month = month < 10 ? "0" + month : month;

        var day = date.getDate();
        day = day < 10 ? "0" + day : day;

        var hour = date.getHours();
        hour = hour < 10 ? "0" + hour : hour;
        var minute = date.getMinutes();
        minute = minute < 10 ? "0" + minute : minute;
        var second = date.getSeconds();
        second = second < 10 ? "0" + second : second;

        return pattern.replace("YYYY", year).replace("MM", month).replace("DD", day).replace("hh", hour).replace("mm", minute).replace("ss", second);


    }


    console.log(DateFormat("YYYY/MM/DD hh:mm:ss"));
    console.log(DateFormat("YYYY-MM-DD hh:mm"));
    console.log(DateFormat("YYYY-MM-DD"));
    console.log(DateFormat("hh:mm:ss"));


    // console.log(DateFormat("YYYY/MM/DD hh:mm:ss", "2025-5-13 15:25:36"));
    // console.log(DateFormat("YYYY-MM-DD hh:mm", "2025-5-13 15:25:36"));
    // console.log(DateFormat("YYYY-MM-DD", "2025-5-13 15:25:36"));
    // console.log(DateFormat("hh:mm:ss", "2025-5-13 15:25:36"));

    console.log(DateFormat("YYYY/MM/DD hh:mm:ss", "2025-5-1 0:0:0"));
    console.log(DateFormat("YYYY-MM-DD hh:mm", "2025-5-1 0:0:0"));
    console.log(DateFormat("YYYY-MM-DD", "2025-5-1 0:0:0"));
    console.log(DateFormat("hh:mm:ss", "2025-5-1 0:0:0"));


</script>

</html>